#include "gamepadsUDP.h"

gamepadsUDP::gamepadsUDP(playersManager* _myPlayersManager,int portReceive,string ipSendTeamA,string ipSendTeamB,int portSend)
{
	myPlayersManager = _myPlayersManager;

	udpConnection.Create();
	udpConnection.Bind(portReceive);
	udpConnection.SetNonBlocking(true);
/*
	udpSendToTeamA.Create();
	udpSendToTeamA.Connect(ipSendTeamA.c_str(),portSend);
	udpSendToTeamA.SetNonBlocking(true);

	udpSendToTeamB.Create();
	udpSendToTeamB.Connect(ipSendTeamB.c_str(),portSend);
	udpSendToTeamB.SetNonBlocking(true);
*/
}

gamepadsUDP::~gamepadsUDP()
{
}


void gamepadsUDP::update()
{
	int udpMessage[4];
	while(udpConnection.Receive((char*)udpMessage,16)==16){
		if(udpMessage[0]!=NULL){
			udpMessage[2]= udpMessage[2]-1;
			cout << udpMessage[0]<< udpMessage[1]<< udpMessage[2]<< udpMessage[3]<< endl;
			if(udpMessage[0]>0 && udpMessage[0]<5)	myPlayersManager->movePlayer(udpMessage[0],udpMessage[1],udpMessage[2]);
			if(udpMessage[0]==5)	myPlayersManager->shootingPlayer(udpMessage[1],udpMessage[2],udpMessage[3]);
			if(udpMessage[0]==6)	myPlayersManager->shootedPlayer(udpMessage[1],udpMessage[2]);
			if(udpMessage[0]==7)	myPlayersManager->shootFailPlayer(udpMessage[1],udpMessage[2]);
		}
	}
}

void gamepadsUDP::sendDeadPlayer(int id,int team, int status)
{
	int package[3];
	package[0] = id;
	package[1] = team;
	package[2] = status;

	if(team==1)
	{
		//udpSendToTeamA.Send((char*)package,12);
	}else{
		//udpSendToTeamB.Send((char*)package,12);
	}
	
}
